import os
import sys
import warnings

import pandas as pd

sys.path.append('..')
warnings.filterwarnings('ignore')

from tools.setting import DATA_DIR

ANALYSIS_DATA_DIR = os.path.join(DATA_DIR, 'analysis')
os.makedirs(ANALYSIS_DATA_DIR, exist_ok=True)
SOCIAL_ANALYSIS_DATA_DIR = os.path.join(ANALYSIS_DATA_DIR, 'sentiment')
os.makedirs(SOCIAL_ANALYSIS_DATA_DIR, exist_ok=True)

def relative_sentiment():
    file_path = os.path.join(DATA_DIR, f'social_data\\sanbase')

    # 计算相对舆情指标
    for channel in ['twitter', 'telegram', 'reddit', 'bitcointalk']:
        file_name_p = os.path.join(file_path, f'sentiment_positive_{channel}')
        sentiment_positive = pd.read_csv(f'{file_name_p}.csv', index_col='end_date')
        file_name_n = os.path.join(file_path, f'sentiment_negative_{channel}')
        sentiment_negative = pd.read_csv(f'{file_name_n}.csv', index_col='end_date')
        relative_sentiment = (sentiment_positive[f'sentiment_positive_{channel}'] -
                              sentiment_negative[f'sentiment_negative_{channel}']) / \
                             (sentiment_positive[f'sentiment_positive_{channel}'] +
                              sentiment_negative[f'sentiment_negative_{channel}'])
        relative_sentiment.name = f'relative_sentiment_{channel}'
        relative_sentiment.fillna(0, inplace=True)
        file_name = os.path.join(SOCIAL_ANALYSIS_DATA_DIR, f'relative_sentiment_{channel}')
        relative_sentiment.to_csv(f'{file_name}.csv')


if __name__ == '__main__':
    relative_sentiment()
